﻿' Copyright © Microsoft Corporation.  All Rights Reserved.
' This code released under the terms of the 
' Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
' <copyright file="ChapterItem.vb" company="Microsoft">
'     Copyright © Microsoft Corporation. All rights reserved.
' </copyright>
' <summary>Implements the ChapterItem class</summary>
' <author>Microsoft Expression Encoder Team</author>

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Windows.Browser
Imports System.Xml

Namespace ExpressionMediaPlayer
	''' <summary>
	''' This class describes a Chapter point in the media stream. Chapter points can
	''' contain a title and a thumbnail among other things.
	''' </summary>
	<ScriptableType> _
	Public Class ChapterItem
		Implements INotifyPropertyChanged, IAccessible
		''' <summary>
		''' top level XML node for this class
		''' </summary>
		Friend Const xmlNode As String = "ChapterItem"
		''' <summary>
		''' The position of the chapter item.
		''' </summary>
		Private m_position As Double
		''' <summary>
		''' The source of the thumbnail for this chapter item.
		''' </summary>
		Private m_thumbSource As Uri
		''' <summary>
		''' The title of this chapter item.
		''' </summary>
		Private m_title As String
		''' <summary>
		''' Event which fires whenever a property changes on this chapter item.
		''' </summary>
		Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
		''' <summary>
		''' initialize chapter item
		''' </summary>
		Private Sub Init()
			m_title = String.Empty
			m_thumbSource = Nothing
		End Sub
		''' <summary>
		''' Initializes a new instance of the ChapterItem class.
		''' </summary>
		Public Sub New()
			Init()
		End Sub
		''' <summary>
		''' Gets or sets the position in the media stream of this chapter item.
		''' </summary>
		<Description("chapter position in seconds")> _
		Public Property Position() As Double
			Get
				Return m_position
			End Get

			Set(ByVal value As Double)
				m_position = value
				OnPropertyChanged("Position")
				OnPropertyChanged("PositionText")
			End Set
		End Property
		''' <summary>
		''' Gets or sets the source of the thumbnail for this chapter item.
		''' </summary>
		<Description("path to chapter thumbnail if required")> _
		Public Property ThumbSource() As Uri
			Get
				Return m_thumbSource
			End Get

			Set(ByVal value As Uri)
				m_thumbSource = value
				OnPropertyChanged("ThumbSource")
			End Set
		End Property
		''' <summary>
		''' Gets or sets the title of this chapter item.
		''' </summary>
		<Description("title text of chapter")> _
		Public Property Title() As String
			Get
				Return m_title
			End Get

			Set(ByVal value As String)
				m_title = value
				OnPropertyChanged("Title")
			End Set
		End Property
		''' <summary>
		''' Gets the position of this chapter item as a string.
		''' </summary>
		Public Overridable ReadOnly Property PositionText() As String
			Get
				Return TimeCode.ConvertToString(m_position, SmpteFrameRate.Unknown)
			End Get
		End Property
		''' <summary>
		''' accessibility text, will be read out by screen reader if your listbox item has a control 
		''' AutomationProperties.Name bound to this
		''' </summary>
		Private ReadOnly Property AccessibilityText() As String Implements IAccessible.AccessibilityText
			Get
				Return String.Format(CultureInfo.InvariantCulture, ExpressionMediaPlayer.Resources.readerChapterItem, New TimeSpan(CLng(Fix(Position)) * 10000000).ReadableTime(), Title)
			End Get
		End Property
		#Region "INotifyPropertyChanged Members"

		''' <summary>
		''' Implements INotifyPropertyChanged.OnPropertyChanged().
		''' </summary>
		''' <param name="memberName">The name of the property that changed.</param>
		Protected Sub OnPropertyChanged(ByVal memberName As String)
			RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
		End Sub
		#End Region
		''' <summary>
		''' deserialise this object
		''' </summary>
		''' <param name="reader">XmlReader to deserialize from</param>
		''' <returns>this</returns>
		Friend Function Deserialize(ByVal reader As XmlReader) As ChapterItem
			If Not reader.IsStartElement(ChapterItem.xmlNode) Then
				Throw New InvalidPlaylistException()
			End If

			reader.Read()
			Do While Not(reader.Name = ChapterItem.xmlNode AndAlso reader.NodeType = XmlNodeType.EndElement)
				If reader.IsStartElement("Position") Then
					Me.Position = reader.ReadElementContentAsDouble()
				ElseIf reader.IsStartElement("ThumbSource") Then
					Me.ThumbSource = New Uri(reader.ReadElementContentAsString(), UriKind.RelativeOrAbsolute)
				ElseIf reader.IsStartElement("Title") Then
					Me.Title = reader.ReadElementContentAsString()
				ElseIf reader.IsStartElement() Then
					Throw New InvalidPlaylistException(reader.Name, ChapterItem.xmlNode)
				ElseIf Not reader.Read() Then
					Exit Do
				End If
			Loop
			Return Me
		End Function
		''' <summary>
		''' serialize this object
		''' </summary>
		''' <param name="writer">XmlWriter to serialze to</param>
		Friend Sub Serialize(ByVal writer As XmlWriter)
			writer.WriteStartElement(ChapterItem.xmlNode)
			writer.WriteElementString("Position", Me.Position.ToString(CultureInfo.InvariantCulture))
			If Me.ThumbSource IsNot Nothing Then
				writer.WriteElementString("ThumbSource", Me.ThumbSource.ToString())
			End If
			writer.WriteElementString("Title", Me.Title)
			writer.WriteEndElement()
		End Sub
	End Class
End Namespace
